<template>
  <n-modal v-model:show="isVisible" title="添加管理员" preset="dialog" @close="onClose"
           style="height: 470px;width: 600px">
    <n-form @submit.prevent="handleSubmit" label-width="120px" class="form-container">

      <div class="form-row" style="margin-top: 20px">
        <n-form-item label="用户名：" path="username">
          <n-input v-model:value="form.username" placeholder="请输入用户名" style="width: 260px"/>
        </n-form-item>
        <n-form-item label="姓名：" path="name">
          <n-input v-model:value="form.name" placeholder="请输入姓名" style="width: 260px"/>
        </n-form-item>
      </div>
      <div class="form-row">
        <n-form-item label="角色：" path="role">
          <n-select v-model:value="form.role"
                    :options="roleOptions"
                    placeholder="选择角色"
                    style="width: 260px"
                    filterable/>
        </n-form-item>

        <n-form-item label="电话：" path="phone">
          <n-input v-model:value="form.phone" placeholder="请输入电话" style="width: 260px"/>
        </n-form-item>
      </div>

      <div class="form-row">
        <n-form-item label="邮箱：" path="email">
          <n-input v-model:value="form.email" placeholder="请输入邮箱" style="width: 260px"/>
        </n-form-item>
      </div>

      <!-- 提交按钮 -->
      <div class="submit-row">
        <n-form-item>
          <n-button type="primary" native-type="submit" @click="handleSubmit">提交</n-button>
        </n-form-item>
      </div>
    </n-form>
  </n-modal>
</template>

<script setup>
import {onMounted, ref, watch} from 'vue';
import {useRouter} from 'vue-router';
import {useUserStore} from "@/store/index.js";
import api from '@/api/admin'

const router = useRouter()
const loading = ref(true)
const userStore = useUserStore()
const id = userStore.getId();
const role = userStore.getRole();
const adminRole = role.toString();
const emit = defineEmits(['updatePage'])
const props = defineProps({
  show: {
    type: Boolean,
    required: true,
  },
  onClose: {
    type: Function,
    required: true,
  },
  roleOptions:{
    type: Array,
    required: true,
  }
});

const isVisible = ref(props.show);
watch(() => props.show, (newVal) => {
  isVisible.value = newVal;
});

const form = ref({
  name: '',
  username: '',
  role: '',
  phone: '',
  email: '',
});

const handleSubmit = async () => {
  try {
    const res = await api.addAdmin(
        {
          ...form.value,
          adminId: id,
        }
    )
    if (res.code === 200) {
      emit("updatePage")
    } else {
      $message.error(res.message)
    }
  } catch (err) {
    $message.error("添加失败！")
  } finally {
    clear()
    props.onClose();
  }
};

const clear = () => {
  form.value.name = '';
  form.value.username = '';
  form.value.role = '';
  form.value.phone = '';
  form.value.email = '';
}

</script>

<style scoped>
.form-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
  width: 100%;
  position: relative;
}

.form-row {
  display: flex;
  gap: 20px;
  align-items: center;
  width: 100%;
}

.form-row > n-form-item {
  flex: 1;
}

.submit-row {
  justify-content: center;
  display: flex;
  width: 100%;
}
</style>
